import { addNotice, getGradeMajor } from "../../utils/request/api";
import msg from "../../utils/msg";
import { formatTime } from '../../utils/util'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addContent: {
      title: '' as string,
      time: '' as string,
      people: '' as string,
      content: '' as string,
      grade: '' as string,
      major: '' as string
    },
    classes: [] as Array<{ text: string, value: string | number, children: Array<{ text: string, value: string | number }> }>,
    columns: [
      {
        values: [] as Array<string>,
        className: 'column1',
      },
      {
        values: [] as Array<string>,
        className: 'column2',
        defaultIndex: 0,
      },
    ],
    show: false,
    showtime: false,
    minHour: 0,
    maxHour: 24,
    currentDate: new Date().getTime(),
  },

  //提交
  submit: async function () {
    msg.Loading('正在发布');
    await addNotice(this.data.addContent).then(() => { wx.navigateBack(); });
  },

  //获取输入
  getInput(e: any) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      ['addContent.' + index]: e.detail.value,
    });
  },

  //获取发布时间
  getInputTime(event: any) {
    let addContent = this.data.addContent;
    addContent.time = formatTime(new Date(event.detail));
    this.setData({
      addContent: addContent,
      showtime: false,
    });
  },

  //多级选择完成
  onFinish(e: any) {
    let grade = e.detail.selectedOptions[0].text;
    let major = e.detail.selectedOptions[1].text;
    let addContent = this.data.addContent;
    addContent.grade = grade;
    addContent.major = major;
    this.setData({
      addContent: addContent,
    });
  },

  //一级选择完成
  onClickTab(e: any) {
    let grade = e.detail.selectedOptions[0].text;
    let addContent = this.data.addContent;
    addContent.grade = grade;
    addContent.major = '';
    this.setData({
      addContent: addContent,
    });
  },

  //关闭弹窗
  onClose() {
    this.setData({
      show: false,
      showtime: false,
    })
  },

  //选择班级弹窗
  onChoose() {
    this.setData({
      show: true,
    })
  },

  //选择发布时间弹窗
  onChooseTime() {
    this.setData({
      showtime: true,
    })
  },

  //返回
  onClickLeft() {
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function () {
    let self = this;
    //拿取年级-专业数据
    await getGradeMajor()
      .then((res: any) => {
        self.setData({
          classes: res.data.data,
        })
      });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})